<template>
    <view class="container">
        <slot></slot>
    </view>
</template>

<script>
export default {
    unicomGroup: ['collapse-item'],
    data() {
        return {
            _expandItems: []
        };
    },
    options: {
        pureDataPattern: /^_/
    },
    relations: {
        '../collapse-item/index': {
            type: 'child',
            linked: function () {
                this._setAllItemId();
            },
            linkChanged: function () {
                this._setAllItemId();
            },
            unlinked: function () {
                this._setAllItemId();
            }
        }
    },
    mounted() {
        // 处理小程序 ready 生命周期
        this.$nextTick(() => this.ready());
    },
    props: {
        type: {
            type: String,
            default: 'normal'
        },
        expandItemId: {
            type: Array,
            default: () => []
        }
    },
    watch: {
        expandItemId: {
            deep: true,
            immediate: true,

            handler: function () {
                this.updateView();
            }
        }
    },
    methods: {
        ready: function () {
            this.updateView();
        },

        async updateView() {
            let t;
            if ('accordion' === this.type) {
                t = this.expandItemId.slice(0, 1);
            } else {
                t = this.expandItemId;
            }
            let e = this.getRelationNodes('collapse-item');
            for (let a = 0; a < e.length; a++) {
                let d = e[a];
                let i = 'default' === d.data.itemId ? a : d.data.itemId;
                if (t.indexOf(i) > -1 && !d.isExpandContent) {
                    await this.setCollapseItemStatus(d, true);
                } else {
                    if (d.isExpandContent || 'accordion' === this.type) {
                        await this.setCollapseItemStatus(d, false);
                    }
                }
            }
        },

        async onTapCollapseItem(t) {
            if ('accordion' === this.type) {
                await this.foldAllExpandItem(t);
            }
            this.setCollapseItemStatus(t, !t.data.isExpandContent);
            if (t.data.isExpandContent) {
                this.$emit('linfold', {
                    detail: {
                        id: t.data.itemId ? t.data.itemId : t.data._idDefault
                    }
                });
            } else {
                this.$emit('linexpand', {
                    detail: {
                        id: t.data.itemId ? t.data.itemId : t.data._idDefault
                    }
                });
            }
        },

        async setCollapseItemStatus(t, e) {
            if (e) {
                t.expandContent();
                this._expandItems.push(t);
            } else {
                await t.foldContent();
                for (let e = 0; e < this._expandItems.length; e++) {
                    if (this._expandItems[e] === t) {
                        this._expandItems.splice(e, 1);
                    }
                }
            }
        },

        foldAllExpandItem(t) {
            for (let e = 0; e < this._expandItems.length; e++) {
                if (t !== this._expandItems[e]) {
                    this._expandItems[e].foldContent();
                }
            }
            this._expandItems = [];
        },

        _setAllItemId() {
            this.getRelationNodes('collapse-item').forEach((t, e) => {
                t.data._idDefault = e;
            });
        }
    },
    created: function () {}
};
</script>
<style></style>
